# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import os

from itemadapter import ItemAdapter
import pymysql
from .settings import *

class NovelbooksPipeline:
    def open_spider(self, spider):
        self.db = pymysql.connect(
            MYSQL_HOST,MYSQL_USER,MYSQL_PWD,MYSQL_DB,
            charset=MYSQL_CHAR
        )
        self.cursor = self.db.cursor()

    def process_item(self,item,spider):
        ins = 'insert into book values(null,%s,%s,%s,%s,%s,%s,%s,%s)'
        L = [
            item["typeName"],
            item["bookName"],
            item["title"],
            item["author"],
            item["time"],
            item["description"],
            item["status"],
            item["content"]
        ]
        self.cursor.execute(ins,L)
        self.db.commit()
        # saveTxt(item)
        return item

    def close_spider(self,spider):
        self.cursor.close()
        self.db.close()


# 存储本地txt文件
# windows 上路径为 \  mac上为  /
def saveTxt(item):

    #windows 系统路径
    books = os.getcwd() + '\\books'
    if not os.path.exists(books):
        os.makedirs(os.getcwd() + '\\books')
    # 生成小说类型文件夹
    typeFile = books + '\{}'.format(item['typeName'])
    if not os.path.exists(typeFile):
        os.makedirs(typeFile)
    # 小说名称文件夹
    bookFile = typeFile + '\{}'.format(item['bookName'])
    if not os.path.exists(bookFile):
        os.makedirs(bookFile)
    filename = bookFile + '\{}.txt'.format(item['title'])
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(item['content'])

    # macos 系统路径
    # books = os.getcwd() + '/books'
    # if not os.path.exists(books):
    #     os.makedirs(os.getcwd() + '/books')
    # # 生成小说类型文件夹
    # typeFile = books + '/{}'.format(item['typeName'])
    # if not os.path.exists(typeFile):
    #     os.makedirs(typeFile)
    # # 小说名称文件夹
    # bookFile = typeFile + '/{}'.format(item['bookName'])
    # if not os.path.exists(bookFile):
    #     os.makedirs(bookFile)
    # filename = bookFile + '/{}.txt'.format(item['title'])
    # with open(filename, 'w', encoding='utf-8') as f:
    #     f.write(item['content'])

